layui.config({
	base: '/static/plugin/build/js/'
}).use(['ztree', 'layer', 'form', 'table'], function() {
	var $ = layui.$;
	var layer = layui.layer;
	var form = layui.form;
	var grid = layui.table;
	var url=window.location.search;
	var classtype = url.substring(url.lastIndexOf('=')+1, url.length); 
	var jsonurl;
	if(classtype==1){
		jsonurl='knowledgeFileList.json';
		jsontable='knowledgeFileTable.json';
	}else{
		jsonurl='downloadFileList.json';
		jsontable='downloadFileTable.json';
	}
	var setting = {
		view: {
			showLine: true,
			showTitle: true,
			selectedMulti: false
		},
		edit: {
			enable: false
		},
		data: {
			simpleData: {
				enable: true
			}
		},
		callback: {
			onClick: trainingTreeClick,
		}
	}
	//加载资源树
	loadDeptTree();
	var operFlag = "view";
	var trainingID = 1;

	function loadDeptTree() {
		$.get(
			"/mock/"+jsonurl,
			function(result) {
				if(result.code == 0) {
					var zNodes = result.data;
					//加载tree
					$.fn.zTree.init($("#ztree"), setting, zNodes);
					//根节点被选中
					var treeObj = $.fn.zTree.getZTreeObj("ztree");
					var node = treeObj.getNodeByParam("id", 0, null);
					treeObj.selectNode(node);
					trainingTreeClick(event, "ztree", node);
				}
			},
			"json"
		);
	}
	//加载表格
	initMyGrid();

	function initMyGrid() {
		grid.render({
			elem: '#trainingGrid', //指定原始表格元素选择器（推荐id选择器）
			url: '/mock/'+jsontable, //请求地址
			cols: [
				[{
						field: 'filename',
						title: '文件名',
						width: '15%'
					},
					{
						field: 'filesize',
						title: '文件大小',
						width: '15%',
						templet: '#fileSizeSwitch'
					},
					{
						field: 'createtime',
						title: '上传时间',
						width: '15%'
					},
					{
						field: 'createusername',
						title: '上传人',
						width: '15%'
					},
					{
						field: 'createorganizationname',
						title: '上传单位',
						width: '20%'
					},
					{
						title: '操作',
						width: '20%',
						fixed: 'right',
						align: 'center',
						toolbar: '#handle'
					}
				]
			], //设置表头
			id: 'table_reload', //设定容器唯一ID
			page: true, //开启分页
			limits: [10, 20, 50, 100], //每页数量
			limit: 10, //默认每页数量
			height: 'full-60', //容器高度
			where: {
				"trainingID": trainingID
			},
			method: 'get',
			even: true //隔行换色
		});
	}

	function gridReload() {
		grid.reload('table_reload', {
			where: {
				"trainingID": trainingID
			}
		});
	}

	function trainingTreeClick(event, treeId, treeNode) {
		if(treeNode) {
			trainingID = treeNode.id;
			gridReload();
		}
	}

});